const express = require("express");
const cors = require('cors')
const pool = require('./pool')
let server = express();
const bodyparser = require("body-parser");
server.use(bodyparser.urlencoded({ extended: false, }));
server.use(cors({
    origin: ['http://xiaoshuoge.applinzi.com', 'https://xiaoshuoge.applinzi.com', 'http://3.xiaoshuoge.applinzi.com']
}))
//模糊查询小说或者作者
server.get('/select', (req, res) => {
    var $value = "%" + req.query.value + "%";

    pool.query("select * from travel_books where author LIKE ? or title LIKE ?", [$value, $value], (err, results) => {
        if (err) throw err;
        // res.set('Access-Control-Allow-Origin', '*')
        res.send({ message: '查询成功', code: 1, results: results })
    });

});
//查看书架
server.get('/bookrack', (req, res) => {

    let sql = "select * from travel_bookrack";
    pool.query(sql, (err, results) => {
        if (err) throw err;
        // res.set('Access-Control-Allow-Origin', '*')
        res.send({ message: '查询成功', code: 1, results: results })
    })
})

//删除小说
server.get('/delete_book', (req, res) => {
    let title = req.query.title
    let sql = "delete from travel_bookrack where title=?";
    pool.query(sql, [title], (err, results) => {
        if (err) throw err;
        // res.set('Access-Control-Allow-Origin', '*')
        res.send({ message: '查询成功', code: 1, results: results })
    })
})
//查询阅读历史
server.get('/sec_history', (req, res) => {

    let sql = "select * from travel_history";
    pool.query(sql, (err, results) => {
        if (err) throw err;
        // res.set('Access-Control-Allow-Origin', '*')
        res.send({ message: '查询成功', code: 1, results: results })
    })
})
//添加小说到阅读历史
server.get('/book_history', (req, res) => {
    let title = req.query.title
    let sql = "delete from travel_history where title=?";
    pool.query(sql, [title], (err, results) => {
        if (err) throw err;
        console.log(results)
        pool.query("select bid,title,cover,majorCate,minorCate,author,shortIntro from travel_books where title=?", [title], (err, results) => {
            if (err) throw err;
            let bid = results[0].bid;
            let title = results[0].title;
            let cover = results[0].cover;
            let majorCate = results[0].majorCate;
            let minorCate = results[0].minorCate;
            let author = results[0].author;
            let shortIntro = results[0].shortIntro;
            if (results.length > 0) {
                let sql1 = 'insert into travel_history(bid,title,cover,majorCate,minorCate,author,shortIntro) VALUES(?,?,?,?,?,?,?)';

                pool.query(sql1, [bid, title, cover, majorCate, minorCate, author, shortIntro], (err, results) => {
                    if (err) throw err;
                    let sql2 = "select * from travel_bookrack"
                    pool.query(sql2, (err, results) => {
                        if (err) throw err;
                        // res.set('Access-Control-Allow-Origin', '*')
                        res.send({ message: '加入成功', code: 1, results: results })
                    })
                })
            } else {
                // res.set('Access-Control-Allow-Origin', '*')
                res.send({ message: "加入失败", code: 0 })
            }
        });

    })



});
//添加小说到书架
server.get('/addbook', (req, res) => {
    let title = req.query.title

    pool.query("select bid,title,cover from travel_books where title=?", [title], (err, results) => {
        if (err) throw err;
        let bid = results[0].bid;
        let title = results[0].title;
        let cover = results[0].cover;
        if (results.length > 0) {
            let sql1 = 'insert into travel_bookrack(bid,title,cover) VALUES(?,?,?)';

            pool.query(sql1, [bid, title, cover], (err, results) => {
                if (err) throw err;
                let sql2 = "select * from travel_bookrack"
                pool.query(sql2, (err, results) => {
                    if (err) throw err;
                    // res.set('Access-Control-Allow-Origin', '*')
                    res.send({ message: '查询成功', code: 1, results: results })
                })
            })
        } else {
            // res.set('Access-Control-Allow-Origin', '*')
            res.send({ message: "加入失败", code: 0 })
        }
    });

});
//查询小说章节详情
server.get('/cate_sec', (req, res) => {
    let num_id = req.query.num_id
    let title = req.query.title
    let sql = "select * from travel_cate where title=? and num_id=?";
    pool.query(sql, [title, num_id], (err, results) => {
        if (err) throw err;
        // res.set('Access-Control-Allow-Origin', '*')
        res.send({ message: '查询成功', code: 1, results: results })
    })
})
//小说分类查询
server.get('/major', (req, res) => {
    let majorCate = req.query.majorCate
    let minorCate = req.query.minorCate
    let sql = "select * from travel_books where majorCate=? and minorCate=?";
    pool.query(sql, [majorCate, minorCate], (err, results) => {
        if (err) throw err;
        // res.set('Access-Control-Allow-Origin', '*')
        res.send({ message: '查询成功', code: 1, results: results })
    })
})
//查询小说章节
server.get('/cate', (req, res) => {
    let title = req.query.title
    let sql = "select * from travel_cate where title=?";
    pool.query(sql, [title], (err, results) => {
        if (err) throw err;
        // res.set('Access-Control-Allow-Origin', '*')
        res.send({ message: '查询成功', code: 1, results: results })
    })
})
//小说排行榜
server.get('/male', (req, res) => {
    let sql = "select mid,title,cover from travel_male";
    pool.query(sql, (err, results) => {
        if (err) throw err;
        // res.set('Access-Control-Allow-Origin', '*')
        res.send({ message: '查询成功', code: 1, results: results })
    })
})
//轮播图
server.get('/banner', (req, res) => {
    let sql = "select * from travel_banner";
    pool.query(sql, (err, results) => {
        if (err) throw err;
        // res.set('Access-Control-Allow-Origin', '*')
        res.send({ message: '查询成功', code: 1, results: results })
    })
})
//查询所有小说
server.get('/books', (req, res) => {

    let sql = "select * from travel_books";
    pool.query(sql, (err, results) => {
        if (err) throw err;
        // res.set('Access-Control-Allow-Origin', '*')
        res.send({ message: '查询成功', code: 1, results: results })
    })
})
//根据id查询小说
server.get('/book', (req, res) => {
    let mid = req.query.mid
    let sql = "select * from travel_books where male_id=?";
    pool.query(sql, [mid], (err, results) => {
        if (err) throw err;
        // res.set('Access-Control-Allow-Origin', '*')
        res.send({ message: '查询成功', code: 1, results: results })
    })
})
//根据id查询小说详情
server.get('/book_detail', (req, res) => {
    let title = req.query.title
    let sql = "select * from travel_books where title=?";
    pool.query(sql, [title], (err, results) => {
        if (err) throw err;
        // res.set('Access-Control-Allow-Origin', '*')
        res.send({ message: '查询成功', code: 1, results: results })
    })
})
server.post('/reg', (req, res) => {
    let username = req.body.uname;
    let password = req.body.upwd;
    let sql = "select uname from travel_user where uname=?";
    pool.query(sql, [username], (err, result) => {
        if (err) throw err;
        if (result.length == 0) {
            sql = 'insert into travel_user(uname,upwd) VALUES(?,?)';
            pool.query(sql, [username, password], (err, resurt) => {
                if (err) throw err;
                // res.set('Access-Control-Allow-Origin', '*')
                res.send({ message: "注册成功", code: 1 })
            })
        } else {
            // res.set('Access-Control-Allow-Origin', '*')
            res.send({ message: "用户名已经存在", code: 0 })
        }
    })
});
server.post('/login', (req, res) => {
    let username = req.body.uname;
    let password = req.body.upwd;
    let sql = 'select * from travel_user where uname=? and upwd=?';
    pool.query(sql, [username, password], (err, results) => {
        if (err) throw err;
        console.log(results)
        if (results.length == 0) {
            // res.set('Access-Control-Allow-Origin', '*')
            res.send({ message: '错误', code: 0 })
        } else {
            // res.set('Access-Control-Allow-Origin', '*')
            res.send({ message: '登陆成功', code: 1, results: results })
        }
    })
})
server.post('/reset', (req, res) => {
    let username = req.body.uname;
    let password = req.body.upwd;
    let sql = 'update travel_user set upwd=? where uname=?';
    pool.query(sql, [password, username], (err, results) => {
        if (err) throw err;
        console.log(results)
        if (results.affectedRows == 0) {
            // res.set('Access-Control-Allow-Origin', '*')
            res.send({ message: '错误', code: 0 })
        } else {
            // res.set('Access-Control-Allow-Origin', '*')
            res.send({ message: '修改成功', code: 1 })
        }
    })
})
server.listen(5050, () => {
    console.log("server is running...")
})